/* 系统公用方法 */
(function(win, doc) {
    var regEx = {
        Strs: /^[\u0391-\uFFE5\w]+$/, //中文字、英文字母、数字和下划线
        Tel: /^[1|9]\d{10}$/, //放开手机格式验证，1,9开头的11位数字即可
        SpecialWord: /^[`\\~\!\@\#\$\%\^\&\*\(\)\_\+\{\}\|\:\"\<\>\?\/\.\,\;\'\[\]\\]+$/, //检查特殊字符
    };
    var common = {
        
        basePath: function() {
            var curWwwPath = window.document.location.href;
            var pathName = window.document.location.pathname;
            var pos = curWwwPath.indexOf(pathName);
            var localhostPath = curWwwPath.substring(0, pos);
            var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
            var path = localhostPath + projectName + "/";
            return path;
        },
        screen:function () {
            var w = $(window).width();
            return w >= 1200 ? 3 : w >= 992 ? 2 : w >= 768 ? 1 : 0;
        },
        //简写为空判断
        isEmpty: function(str) {
            return (str === '' || str === null) ? false : true;
        },
        //替换html
        htmlReplace: function(str) {
            str = str.replace(/&/g, '＆');
            str = str.replace(/</g, '＜');
            str = str.replace(/>/g, '＞');
            str = str.replace(/\!/g, '！');
            str = str.replace(/\|/g, '｜');
            str = str.replace(/\./g, '．');
            str = str.replace(/\^/g, '＾');
            str = str.replace(/\$/g, '＄');
            str = str.replace(/\&/g, '＆');
            str = str.replace(/\*/g, '＊');
            str = str.replace(/=/g, '＝');
            return str;
        },
        //检查是否有特殊符号
        checkStr: function(str) {
            var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）&mdash;—|{}【】‘；：”“'。，、？]")
            var rs = "";
            for (var i = 0; i < str.length; i++) {
                rs = rs + str.substr(i, 1).replace(pattern, '');
            }
            return rs;
        },
        chkSpcWord: function(str) {
            if (regEx.SpecialWord.test(str)) {
                return true;
            } else {
                return false;
            }
        },
        //检查手机号
        chkTel: function(str) {
            if (regEx.Tel.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //获取长度
        getLength: function(str) {
            var realLength = 0,
                len = str.length,
                charCode = -1;
            for (var i = 0; i < len; i++) {
                charCode = str.charCodeAt(i);
                if (charCode >= 0 && charCode <= 128) realLength += 1;
                else realLength += 2;
            }
            return realLength;
        },
        //截取长度
        cutstr: function(str, len) {
            var str_length = 0;
            var str_len = 0;
            var str_cut = new String();
            var str_len = str.length;
            for (var i = 0; i < str_len; i++) {
                var a = str.charAt(i);
                str_length++;
                if (escape(a).length > 4) {
                    str_length++;
                }
                str_cut = str_cut.concat(a);
                if (str_length >= len) {
                    str_cut = str_cut.concat("...");
                    return str_cut;
                }
            }
            if (str_length < len) {
                return str;
            }
        },
        strTobool: function(str) {
            return (str && typeof str == 'string') ? (str.toLowerCase() == 'true' || str == '1') : (str == true);
        },
        //获取地址 避免地址混乱
        getRootPath: function() {
            //point根据项目目录自己设定
            var location = win.location.href,
                path = location.split(app.serviceName)[0] + app.serviceName;
            return path;
        },
        //随机数
        random: function(length, upper, lower, number) {
            if (!upper && !lower && !number) {
                upper = lower = number = true;
            }
            var a = [
                ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
                ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"],
                ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
            ];
            var b = [];
            var c = "";
            b = upper ? b.concat(a[0]) : b;
            b = lower ? b.concat(a[1]) : b;
            b = number ? b.concat(a[2]) : b;
            for (var i = 0; i < length; i++) {
                c += b[Math.round(Math.random() * (b.length - 1))];
            }
            return c;
        },
        //获取URL参数 key 参数名称 url URL链接，默认为当前URL
        getUrlKey: function(key, url) {
            var url = url ? url : location.href;
            var v = '';
            var o = url.indexOf(key + "=");
            if (o != -1) {
                o += key.length + 1;
                e = url.indexOf("&", o);
                if (e == -1) {
                    e = url.length;
                }
                //v = decodeURIComponent(url.substring(o, e));
                v = url.substring(o, e);
            }
            return v;
        },
        
    };
    win.common = common;
}(window, document));



